output_notebook()
x_value = 'goals'
y_value = 'birth_month'
nhl_player_stats_team_df = nhl_player_stats_team_df.sort_values(by='birth_month_number')
df = nhl_player_stats_team_df[[
y_value,
x_value,
]]
categories = nhl_player_stats_team_df[y_value].unique().tolist()
# find the quartiles and IQR for each category
groups = df.groupby(y_value)
q1 = groups.quantile(q=0.25)
q2 = groups.quantile(q=0.5)
q3 = groups.quantile(q=0.75)
iqr = q3 - q1
upper = q3 + 1.5*iqr
lower = q1 - 1.5*iqr
# if lower bound is less than zero, zero it
def greaterThanZero(x):
return x if (x > 0) else 0
lower[x_value] = lower[x_value].apply(greaterThanZero)
p = figure(width=800, height=400, y_range=categories)
for month in categories:
p.hbar(
y=[month],
height=0.6,
left=q1[x_value][month],
right=q2[x_value][month],
fill_color="#E08E79",
legend_label=month
)
p.hbar(
y=[month],
height=0.6,
left=q3[x_value][month],
right=q2[x_value][month],
fill_color="#E08E79",
legend_label=month
)
p.segment(x0=lower[x_value][month], y0=[month], x1=q1[x_value][month],y1=[month],legend_label=month)
p.segment(x0=q3[x_value][month], y0=[month], x1=upper[x_value][month],y1=[month],legend_label=month)
p.rect(x=lower[x_value][month], y=[month], width=0.1, height=0.8,legend_label=month)
p.rect(x=q2[x_value][month], y=[month], width=0.1, height=0.8,legend_label=month)
p.rect(x=upper[x_value][month], y=[month], width=0.1, height=0.8,legend_label=month)
p.legend.click_policy = "hide"
p.legend.background_fill_alpha = 0.3
# show the results
show(p)
#q1.index.values